<?php

namespace App\Controller\Home;

use AlibabaCloud\SDK\Sample\Sample;
use App\Controller\Example\VcodeExample;
use App\Models\Conn;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;
use Core\Utils\Ip\Ipinit;
use Core\Utils\Message\Mail;
use Core\Utils\Message\Message;
use Core\Utils\Utils;

class Vcode
{
    /**
     * 发送短信验证码
     * entry phone,type
     * return json
     * */
    public function sendCode($data){
        $VcodeExample = Common::assemble(new VcodeExample(), $data);
        if(!in_array($VcodeExample->type,array(1,2,3,4,5,6,7,8,9))){
            Result::error(ErrorEnum::OPERATIONFAILED[$VcodeExample->language]);
        }

        $code = mt_rand(1000,9999);
        $VcodeExample->ip = new Ipinit();
        $VcodeExample->status = 1;
        $conn = new Conn();
        if(!$VcodeExample->phone){
            $VcodeExample->phone = UsersModel::getUserInside($conn, $VcodeExample->token)["username"];
        }
        $sql="select UNIX_TIMESTAMP(`time`) as time,`code` from `vcode` where `phone` = '$VcodeExample->phone' and `type` ='$VcodeExample->type' and `ip` = '$VcodeExample->ip' and `status` = '$VcodeExample->status' order by time desc limit 1";
        $data = $conn->readConn($sql);
        /**
         *短信5分钟内不重复发送
         */
        if((time()-$data['time']) < 300){
            Result::error(ErrorEnum::THEVERIFICATIONCODECANNOTBEOBTAINEDREPEATEDLYWITHIN5MINUTES[$VcodeExample->language]);
        }else{
            $sql="INSERT INTO `vcode`(`phone`, `code`, `type`,`status`,`ip`) VALUES ('$VcodeExample->phone','$code',$VcodeExample->type,'$VcodeExample->status','$VcodeExample->ip')";
            $conn->shiwuStart();
            if($conn->executeConn($sql) !=1){
                $conn->shiwuRollback();
                Result::error(ErrorEnum::THEDATAISBNORMALPLEASELOGOUT[$VcodeExample->language]);
            }
            if(!Utils::phoneChecking($VcodeExample->phone)){
                if(!Utils::emailChecking($VcodeExample->phone)){
                    Result::error(ErrorEnum::ACCOUNTFORMATERROR[$VcodeExample->language]);
                }else{
                    self::emailSend($VcodeExample->phone,$code,1,$VcodeExample->language);
                }
            }else{
                self::phoneSend($VcodeExample->phone,$code);
            }
            $conn->shiwuEnd();
            Result::success(ErrorEnum::SENDSUCCESS[$VcodeExample->language].$VcodeExample->phone);
        }
    }
    /**
     * 验证短信验证码
     * entry VcodeExample
     * return json
     * */
    public function verification($VcodeExample){
        $VcodeExample->ip = new Ipinit();
        $conn = new Conn();
        $sql="select `code` from `vcode` where `phone` = '$VcodeExample->phone' and `type` ='$VcodeExample->type' and `ip` = '$VcodeExample->ip' and `status` = '$VcodeExample->status' order by time desc limit 1";
        $data = $conn->readConn($sql);
        return $data["code"];
    }
    public static function phoneSend($phone,$code,$type=1){
        $message = new Message();
        $message->sendMessage($phone, $code,$type);
        $args["phone"] = $phone;
        $args["code"] = $code;
        $args["type"] = $type;
        Sample::main($args);
        //if($sendMessage['status'] != 0) Result::error(ErrorEnum::FAILINSEND[$VcodeExample->language]);
    }
    /**
     * c2c推送短信
     * entry $email $code
     * return json
     * */
    public static function TctSend($username,$code,$type){
        $VcodeExample = new VcodeExample();
        $VcodeExample->phone =$username;
        $VcodeExample->code =$code;
        $VcodeExample->type = $type;
        if(!Utils::phoneChecking($VcodeExample->phone)){
            if(!Utils::emailChecking($VcodeExample->phone)){
                //Result::error(ErrorEnum::FAILINSEND[$VcodeExample->language]);
            }else{
                self::emailSend($VcodeExample->phone,$VcodeExample->code,$VcodeExample->type);
            }
        }else{
            self::phoneSend($VcodeExample->phone,$VcodeExample->code,$VcodeExample->type);
        }
    }
    /**
     * 发送邮箱验证码
     * entry $email $code
     * return json
     * */
    public static function emailSend($email,$code,$type=1,$language=0){
        switch ($type){
            case 1://通用验证码
                $content="【Jiujiu】 Your ID processing verification {$code} is under review now. PLEASE DO NOT share it with anyone for account security, wish nice day! \r\n \r\n【玖玖】您的帐户正在处理的验证码是: {$code} 进行身份验证，打死不告诉别人！ ";
                $subject = "验证码 Verification code";
                break;
            case 2://订单匹配
                $content="【Jiujiu】您发布的广告已经被{$code}用户成功下单，请尽快处理，否则超过20分钟系统将会自动取消订单，感谢您的配合！";
                $subject = "通知";
                break;
            case 3://确认支付
                $content="【Jiujiu】您的订单已被{$code}用户确认支付，请尽快核对清楚后再确认放行，因个人失误导致的损失，后果自负，感谢您的配合！";
                $subject = "通知";
                break;
            case 4://确认放行
                $content="【Jiujiu】您的订单已被{$code}用户确认放行，该笔订单已完成结束，因个人失误导致的损失，后果自负，感谢您的配合！";
                $subject = "通知";
                break;
            default:
                $content="【Jiujiu】您的验证码 Your verification code is: {$code}，您正进行身份验证，打死不告诉别人！ ,your ID verification is under review now. PLEASE DO NOT share it with anyone for account security, wish nice day! ";
                $subject = "验证码 Verification code";
        }
        $emailArr = [
            "accountName" => "nine@mail.999nineclub.com",//管理控制台中配置的发信地址。
            "addressType" => 1,//地址类型。取值：0：为随机账号1：为发信地址
            "replyToAddress" => false,//是否启用管理控制台中配置好回信地址 true false
            "toAddress" => $email,//目标地址，多个 email 地址可以用逗号分隔，最多 100 个地址
            "subject" => $subject,//邮件主题
// "htmlBody" => $args['htmlBody'] ?? "",//邮件 html 正文，限制 28K。
            "textBody" => $content,//邮件 text 正文，限制 28K。
            "fromAlias" => "Jiujiu"
        ];
        $mail = new Mail();
        $mail::main($emailArr);
    }
}